package co.sugon.log.searchEngineService;


import co.sugon.log.utils.ResourceLoader;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.List;
import java.util.Map;

/**
 * ClassName:   SQLExecuteService.java
 */
public  class SQLExecuteService {

    private static final Logger logger = LoggerFactory.getLogger(SQLExecuteService.class);

    public static String sqlEngine = "";

    private static SQLEngineService service;

    static {
        // 获取引擎，默认使用HIVE
        try {
            sqlEngine = ResourceLoader.getExtraProperties("vmp.sql.engine");
            System.out.println("sql引擎名称：" + sqlEngine);
        } catch (Exception e) {
            e.printStackTrace();
        }
        if (SQLEngineService.SQL_ENGINE_SPARK.equalsIgnoreCase(sqlEngine)) {
            service = new SparkSQLEngineService();
        } else if (SQLEngineService.SQL_ENGINE_PRESTO.equalsIgnoreCase(sqlEngine)) {
            service = new PrestoSQLEngineService();
        } else {
            service = new HiveSQLEngineService();
        }
        System.out.println("sql引擎：" + service.getClass().getCanonicalName());
    }


    /**
     * execute:
     * <p>执行查询
     *
     * @since   v1.0
     */
    public  static List<Map<String, Object>> executeQuery(String sql) {
        return service.executeQuery(sql);
    }

    public static List<Object[]> queryRows(String sql){
        return service.queryRows(sql);
    }

    /**
     *
     * 查询总数
     *
     * */
    public  static long queryCount(String sql){
        return service.getCount(sql);
    }

    public static void main(String[] args){

        String sql="select count(1)count,xm from zhusu_table201710251550101  group by xm limit 10";
        List<Map<String, Object>> retList = SQLExecuteService.executeQuery(sql);
        System.out.println(retList.toString());

        for (Map map:retList){
            System.out.println(map.get("count")+"==="+map.get("xm"));
        }


        System.out.println(SQLExecuteService.queryCount("select count(1),num_id from sg_wdd_result_table group by num_id"));
    }


}
